package com.stu.stusystem.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * @author: cxt
 * @time: 2021/3/10
 * <p>
 * 全局异常捕获
 */
@Slf4j
@ControllerAdvice
public class OverallException {

    /**
     * 处理自定义的业务异常
     */
    @ExceptionHandler(value = ApiException.class)
    @ResponseBody
    public ApiResult<ApiException> custom(HttpServletResponse req, ApiException e) {
        req.setStatus(e.getCode());
        log.error("发生错误！原因是：{}", e.getMessage());
        return ApiResult.error(e.getCode(), e.getMessage());
    }
}
